﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebUI.BL.News;
using WebUI.Models;
namespace WebUI.UserControls
{
    public partial class UCNewsCategory : System.Web.UI.UserControl
    {
        
        protected void Page_Load(object sender, EventArgs e)
        {
            var httpCookie = Request.Cookies["langId"];
            if (httpCookie != null)
            {
                hdfLang.Value = httpCookie.Value;
            }
            else
            {
                hdfLang.Value = "0";
            }
            if(!IsPostBack)
            {

                BindData();
            }
        }

        private void BindData()
        {
            NewsCategoryBL bl = new NewsCategoryBL();
            var source = bl.GetNewsCategoryByLangId(int.Parse(hdfLang.Value))
                .Select(x => new NewsCategoryModelView()
                {
                    No = x.CategoryNo,
                    NameVi = x.CategoryName,
                    NameEn = x.StoreLanguage.Where(n=>n.Attrribute=="Name").Select(n=>n.ValueAttr).FirstOrDefault(),
                    DescriptionVi = x.Description,
                    DescriptionEn = x.StoreLanguage.Where(n => n.Attrribute == "Description").Select(n => n.ValueAttr).FirstOrDefault(),
                }).ToList();
            //them contact
            source.Insert(source.Count, new NewsCategoryModelView
                           {
                               No = 0,
                               NameVi = "Liên hệ",
                               NameEn = "Contact",
                           });

            dtlCategory.DataSource = source;
            dtlCategory.DataBind();
        }



      

        protected void dataList_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if(e.Item.ItemType==ListItemType.Item||e.Item.ItemType==ListItemType.AlternatingItem)
            {
                
                HyperLink li = (HyperLink)e.Item.FindControl("hplCategory");
                var model = (NewsCategoryModelView) e.Item.DataItem;

                if(model.No==0)
                {
                    if (hdfLang.Value == "1")
                        li.Text = model.NameVi;
                    else
                    {
                        li.Text = model.NameEn;
                    }
                    li.NavigateUrl = "../Contact.aspx";
                }
                else
                {
                    li.Text = model.NameEn == null ? model.NameVi : model.NameEn;
                    li.NavigateUrl = "../NewsAdler.aspx?idCategory=" + model.No.ToString();
                }

                

            }
        }
    }
}